<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
    v-el-drag-dialog
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="等级名称" prop="levelName" label-width="100px">
        <el-input v-model="dataForm.levelName" placeholder="参数值"></el-input>
      </el-form-item>
      <el-form-item label="级别" prop="levelRank" label-width="100px">
        <el-input v-model="dataForm.levelRank" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="经验值" prop="levelExp" label-width="100px">
        <el-input v-model="dataForm.levelExp" placeholder="备注"></el-input>
      </el-form-item>
      <el-form-item label="尊享分期值" prop="reward1" label-width="100px">
        <el-input v-model="dataForm.reward1" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
      <el-form-item label="推广收益" prop="reward2" label-width="100px">
        <el-input v-model="dataForm.reward2" placeholder="备注" label-width="100px"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        levelName: "",
        rank: "",
        reward1: "",
        reward2: "",
        levelRank: "",
        levelExp: ""
      },
      dataRule: {
        paramKey: [
          { required: true, message: "参数名不能为空", trigger: "blur" }
        ],
        paramValue: [
          { required: true, message: "参数值不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/member/level/get/${this.dataForm.id}`),
            method: "get",
            params: this.$http.adornParams()
          }).then(({ data }) => {
            // console.log(data)
            if (data && data.status === 200) {
              this.dataForm.levelName = data.data.levelName;
              this.dataForm.rank = data.data.rank;
              this.dataForm.reward1 = data.data.reward1;

              this.dataForm.levelRank = data.data.levelRank;
              this.dataForm.reward2 = data.data.reward2;
              this.dataForm.levelExp = data.data.levelExp;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/member/level/${
                !this.dataForm.id ? "save" : "update/" + this.dataForm.id
              }`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              levelName: this.dataForm.levelName,
              reward2: this.dataForm.reward2,
              reward1: this.dataForm.reward1,
              levelExp: this.dataForm.levelExp,
              levelRank: this.dataForm.levelRank
            })
          }).then(({ data }) => {
            if (data && data.status === 200) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                }
              });
            } else {
              this.$message.error(data.message);
            }
          });
        }
      });
    }
  }
};
</script>
